/*
 * External_Interrupt.c
 *
 * Created: 10/2/2014 8:25:16 PM
 *  Author: Cristinel
 */ 
/*****************************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "General.h"
#include "External_Interrupt.h"
#include "Port.h"
/*****************************************************************************************/
void ExtInt_InitINT0( unsigned char val )
{
    cli();
    // enable INT0 on pin 2
    // set trigger as logic change
    EICRA |= (1<<ISC00);
    // enable/disable the interrupt
    if (val)
    {
        EIMSK |= (1<<INT0);
    } 
    else
    {
        EIMSK &= ~(1<<INT0);
    }
	sei();    
} 
/*****************************************************************************************/
void ExtInt_InitINT1( unsigned char val )
{
    cli();
    // enable INT1 on pin 3
    // set trigger as logic change
    EICRA |= (1<<ISC10);
    // enable/disable the interrupt
    if (val)
    {
        EIMSK |= (1<<INT1);
    } 
    else
    {
        EIMSK &= ~(1<<INT1);
    }
	sei();    
} 
/*****************************************************************************************/
void ExtInt_SelectINT( unsigned char pin, unsigned char val )
{
	// select INT0 or INT1
	if (pin == 3)
	{
		ExtInt_InitINT1(val);
	} 
	else if (pin == 2)
	{
		ExtInt_InitINT0(val);
	}
}
/*****************************************************************************************/
void ExtInt_InitPCINT_PortB( unsigned char pin, unsigned char state )
{
    cli();
    // enable ext interrupts on PORT B -> PCINT7..0 -> pins:D8..13
    PCICR |= (1<<PCIE0);
    // enable/disable the interrupt foe each PIN
    switch (pin)
    {
    case 0:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT0);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT0);
	    }
    	break;
    case 1:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT1);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT1);
	    }
    	break;
    case 2:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT2);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT2);
	    }
    	break;		
    case 3:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT3);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT3);
	    }
    	break;
	case 4:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT4);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT4);
	    }
    	break;
	case 5:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT5);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT5);
	    }
    	break;
	case 6:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT6);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT6);
	    }
    	break;
	case 7:
	    if (state)
	    {
			PCMSK0 |= (1 << PCINT7);
	    } 
	    else
	    {
			PCMSK0 &= ~(1 << PCINT7);
	    }
    	break;
	case 0xFF:
	    if (state)
	    {
			PCMSK0 = 0xFF;
	    } 
	    else
	    {
			PCMSK0 = 0x00;
	    }
	    break;
	default:
		break;
    }
	
	sei();
}
/*****************************************************************************************/
//ISR(PCINT0_vect)
//{
	//
//}
///*****************************************************************************************/
/* Alert !!!
    ISR(INT0_vect) declared in Temp_Hum.c file
*/